
package org.petstore.web.servlet;

import org.petstore.domain.Account;
import org.petstore.persistence.OrderDAO;
import org.petstore.persistence.ShoppingCartDAO;
import org.petstore.persistence.impl.OrderDAOImpl;
import org.petstore.persistence.impl.ShoppingCartDAOImpl;
import org.petstore.service.impl.OrderServiceImpl;
import org.petstore.util.Cart;
import org.petstore.domain.Order;
import org.petstore.persistence.ItemDAO;
import org.petstore.persistence.impl.ItemDAOImpl;
import org.petstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class viewOrderFromOrderListServlet extends HttpServlet {
    private static final String LOGIN_HTML = "/WEB-INF/jsp/account/SignOnForm.jsp";
    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    private static final String LIST_ORDERS = "/WEB-INF/jsp/order/ListOrders.jsp";
    private static final String NEW_ORDER = "/WEB-INF/jsp/order/NewOrderForm.jsp";
    private static final String SHIPPING = "/WEB-INF/jsp/order/ShippingForm.jsp";
    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        OrderDAO orderDAO = new OrderDAOImpl();
        Account account = (Account) session.getAttribute("loginUser");
        Cart cart = (Cart) session.getAttribute("cart");
        Order order = new Order();
        int orderId = Integer.parseInt(request.getParameter("orderId"));


        order = orderDAO.getOrderByOrderId(orderId);
        session.setAttribute("order", order);
        request.getRequestDispatcher(VIEW_ORDER).forward(request, response);
        ShoppingCartDAO shoppingCartDAO = new ShoppingCartDAOImpl();
        shoppingCartDAO.store(account.getUsername(), cart.getCartItemList());
    }
}

